/*
 * JDBCManager.java
 *
 * Created on 15 octobre 2007, 23:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ifips5.nal.epizza.util;

import com.sun.rowset.WebRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.rowset.WebRowSet;





/**
 *
 * @author Moi
 */
public class JDBCManager
{
    private static JDBCManager instance = null;
    private String strDriver = "com.mysql.jdbc.Driver";
    private String strUrl = "jdbc:mysql://localhost/epizza";
    private String strUser = "root";
    private String strPwd = "";
    
    
    public static JDBCManager getInstance()
    {
        if(instance == null)
            instance = new JDBCManager();
        return instance;
    }
    
    
    /** Creates a new instance of JDBCManager */
    private JDBCManager()
    {
    }
    
    private void connectToBD()
    {
        try
        {
            Class.forName(strDriver);
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
    }
    
    
    public Connection getConnection()
    {
        Connection connection = null;
        try
        {
            connection = DriverManager.getConnection(strUrl, strUser, strPwd);
            connection.setAutoCommit(false);
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return connection;
    }
    
    
    
    
    
    public WebRowSet getRowSet(String command, int[] keyCols )
    {
        WebRowSet wrs = null;
        connectToBD();
        try
        {
            wrs = new WebRowSetImpl();
            wrs.setUrl(strUrl);
            wrs.setUsername(strUser);
            wrs.setPassword(strPwd);
            wrs.setCommand(command);
            if(keyCols != null)
                wrs.setKeyColumns(keyCols);
            wrs.execute();
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return wrs;
    }

    public String getStrDriver()
    {
        return strDriver;
    }
    
    
    
    
    
    
    
}
